<?php 
class Zend_Template_Controller{
    private $_controller;
    private $_actions;
    
    public function __construct($controller, $actions){
        $this->_controller = $controller;
        $this->_actions = $actions;
    }
    
    /**
     * Write file 
     * @author tien.nguyen
     */
    public function writeFile(){
        $template = $this->render();
        
        $path = PATH_PROJECT_GENERAL.'/'.
            NAME_PROJECT_GENERAL.'/application/controllers/'. 
            $this->_controller->controller_name.'Controller.php';
        
        $hand = fopen($path, 'w');
        fwrite($hand, $template);
        fclose($hand);
    }
    
    /**
     * render file
     * @author tien.nguyen
     */
    public function render(){
        /*
        $template = $this->readTemplate();
        $template = str_replace('{controller_actions}', $this->renderAction(), $template);
        
        $actionDefaultName = $this->getActionDefault();
        if($actionDefaultName){
            $template = str_replace('{action_uri_default}', $actionDefaultName, $template);
        }        
        
        foreach($this->_controller as $key => $val){
            $template = str_replace('{'.$key.'}', $val, $template);
        }
        
        return $template;
        */
        
        global $smarty;
        $smarty->assign('controller', $this->_controller);
        $smarty->assign('actions', $this->_actions);
        $smarty->assign('template_path', PATH_STRUCTURE.'/zend-framework/templates');
        return $smarty->fetch(PATH_STRUCTURE.'/zend-framework/templates/controller.tpl');
    }
    
    /**
     * set view
     * @param array $actionForms
     * @author tien.nguyen
     * @return string
     */
    public function getSetViewForAction($actionForms){
        $str = "";
        foreach($actionForms as $actionForm){
            $str .= '$this->view->'.$actionForm->form_variable.' = $'.$actionForm->form_variable.";\n";
        }
        return $str;
    }
    
    /**
     * Get action default
     * @author tien.nguyen
     */
    public function getActionDefault(){
        foreach($this->_actions as $action){
            if($action->action_default == 1){
                return $action->action_uri;
            }
        }
        return "";
    }
    
    /**
     * render action
     * @author tien.nguyen
     */
    public function renderAction(){
        $str = "";
        global $smarty;
        
        foreach($this->_actions as $action){
            $smarty->assign('action', $action);
            
            $str .= $smarty->fetch(PATH_STRUCTURE.'/zend-framework/templates/controllers/action.tpl');
            
            /*
            $strTmp = $this->readActionTemplate($action->action_type_template);
            
            //replace action params
            $strTmp = str_replace('{action_params}', $this->renderActionParams($action->params), $strTmp);
            
            //replace action form
            $strTmp = str_replace('{action_forms}', $this->renderActionForm($action->forms), $strTmp);
            
            foreach($action as $key => $val){
                if(is_string($val)){    
                    $strTmp = str_replace('{'.$key.'}', $val, $strTmp);
                }
            }
            
            //string replace set views
            $strTmp = str_replace('{set_view}', $this->getSetViewForAction($action->forms), $strTmp);
            
            $str .= $strTmp;
            */
        }
        return $str;
    }
    
    /**
     * Render action form
     * @author tien.nguyen
     * @param array $actionForms
     * @return string
     */
    public function renderActionForm($actionForms){
        $str = "";
        foreach($actionForms as $actionForm){
            $template = $this->readActionFormTemplate($actionForm->form_type_class);
            //assign params
            foreach($actionForm as $key => $val){
                $template = str_replace('{'.$key.'}', $val, $template);
            }
            $str .= $template;
        }
        return $str;
    }
    
    /**
     * render parmas
     * @author tien.nguyen
     */
    public function renderActionParams($params){
        $str = "";
        foreach($params as $param ){
            $str .= '$'.$param->action_param_variable.' = $this->_getParam(\''.$param->action_param_name.'\', '.$param->action_param_default.');'."\n";
        }
        return $str;
    }
    
    /**
     * Read action template form
     * @author tien.nguyen
     * @param string $actionFormTemplate
     * @return string
     */
    public function readActionFormTemplate($actionFormTemplate){
        return $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/controllers/actions/forms/'.$actionFormTemplate.'.tpl');
    }
    
    /**
     * reader action template
     * @author tien.nguyen
     */
    public function readActionTemplate($actionTemplate){
        //if(empty($actionTemplate)){
            $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/controllers/action.tpl');
        /*
        }else{
            $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/controllers/actions/'.$actionTemplate.'.tpl');
        }*/
        return $template; 
    }
    
    /**
     * read template
     * @author tien.nguyen
     */
    public function readTemplate(){
        $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/controller.tpl');
        return $template; 
    }
}